<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
    <el-form-item label="收件人姓名" prop="name">
      <el-input v-model="dataForm.name" placeholder="收件人姓名"></el-input>
    </el-form-item>
    <el-form-item label="国家" prop="country">
      <el-input v-model="dataForm.country" placeholder="国家"></el-input>
    </el-form-item>
    <el-form-item label="电话" prop="mobile">
      <el-input v-model="dataForm.mobile" placeholder="电话"></el-input>
    </el-form-item>
    <el-form-item label="城市" prop="city">
      <el-input v-model="dataForm.city" placeholder="城市"></el-input>
    </el-form-item>
    <el-form-item label="备用电话" prop="province">
      <el-input v-model="dataForm.province" placeholder="备用电话"></el-input>
    </el-form-item>
    <el-form-item label="邮编" prop="zipcode">
      <el-input v-model="dataForm.zipcode" placeholder="邮编"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="address">
      <el-input v-model="dataForm.address" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="是否默认地址" prop="isdefault">
      <el-radio v-model="dataForm.isdefault" label="0">是</el-radio>
      <el-radio v-model="dataForm.isdefault" label="1">否</el-radio>

    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        radio: '0',
        visible: false,
        dataForm: {
          id: 0,
          uid: '',
          name: '',
          country: '',
          mobile: '',
          city: '',
          province: '',
          zipcode: '',
          address: '',
          isdefault: ''
        },
        dataRule: {
          name: [
            { required: true, message: '收件人姓名不能为空', trigger: 'blur' }
          ],
          country: [
            { required: true, message: '国家不能为空', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '电话不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '城市不能为空', trigger: 'blur' }
          ],
          zipcode: [
            { required: true, message: '邮编不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/address_list/v9addresslist/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.uid = data.v9AddressList.uid
                this.dataForm.name = data.v9AddressList.name
                this.dataForm.country = data.v9AddressList.country
                this.dataForm.mobile = data.v9AddressList.mobile
                this.dataForm.city = data.v9AddressList.city
                this.dataForm.province = data.v9AddressList.province
                this.dataForm.zipcode = data.v9AddressList.zipcode
                this.dataForm.address = data.v9AddressList.address
                this.dataForm.isdefault = data.v9AddressList.isdefault
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/address_list/v9addresslist/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'uid': this.dataForm.uid,
                'name': this.dataForm.name,
                'country': this.dataForm.country,
                'mobile': this.dataForm.mobile,
                'city': this.dataForm.city,
                'province': this.dataForm.province,
                'zipcode': this.dataForm.zipcode,
                'address': this.dataForm.address,
                'isdefault': this.dataForm.isdefault
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
